2 ==============================================================================
4 This file is part of the JUCE library - "Jules' Utility Class Extensions"
5 Copyright 2004-11 by Raw Material Software Ltd.
7 ------------------------------------------------------------------------------
9 JUCE can be redistributed and/or modified under the terms of the GNU General
10 Public License (Version 2), as published by the Free Software Foundation.
11 A copy of the license is included in the JUCE distribution, or can be found
12 online at www.gnu.org/licenses.
14 JUCE is distributed in the hope that it will be useful, but WITHOUT ANY
15 WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
16 A PARTICULAR PURPOSE. See the GNU General Public License for more details.
18 ------------------------------------------------------------------------------
20 To release a closed-source product which uses JUCE, commercial licenses are
21 available: visit www.rawmaterialsoftware.com/juce for more information.
23 ==============================================================================
26 #ifndef __JUCER_GENERICCOMPONENTHANDLER_JUCEHEADER__
27 #define __JUCER_GENERICCOMPONENTHANDLER_JUCEHEADER__
30 //==============================================================================
33 class GenericComponent
: public Component
37 : Component ("new component"),
38 actualClassName ("Component")
42 void paint (Graphics
& g
)
44 g
.fillAll (Colours::white
.withAlpha (0.25f
));
46 g
.setColour (Colours::black
.withAlpha (0.5f
));
47 g
.drawRect (0, 0, getWidth(), getHeight());
48 g
.drawLine (0.0f
, 0.0f
, (float) getWidth(), (float) getHeight());
49 g
.drawLine (0.0f
, (float) getHeight(), (float) getWidth(), 0.0f
);
52 g
.drawText (actualClassName
, 0, 0, getWidth(), getHeight() / 2, Justification::centred
, true);
55 void setClassName (const String
& newName
)
57 if (actualClassName
!= newName
)
59 actualClassName
= newName
;
64 void setParams (const String
& newParams
)
66 if (constructorParams
!= newParams
)
68 constructorParams
= newParams
;
73 String actualClassName
, constructorParams
;
76 //==============================================================================
79 class GenericComponentHandler
: public ComponentTypeHandler
82 //==============================================================================
83 GenericComponentHandler()
84 : ComponentTypeHandler ("Generic Component", "GenericComponent", typeid (GenericComponent
), 150, 24)
87 //==============================================================================
88 Component
* createNewComponent (JucerDocument
*)
90 return new GenericComponent();
93 XmlElement
* createXmlFor (Component
* comp
, const ComponentLayout
* layout
)
95 XmlElement
* e
= ComponentTypeHandler::createXmlFor (comp
, layout
);
96 e
->setAttribute ("class", ((GenericComponent
*) comp
)->actualClassName
);
97 e
->setAttribute ("params", ((GenericComponent
*) comp
)->constructorParams
);
102 bool restoreFromXml (const XmlElement
& xml
, Component
* comp
, const ComponentLayout
* layout
)
104 if (! ComponentTypeHandler::restoreFromXml (xml
, comp
, layout
))
107 ((GenericComponent
*) comp
)->actualClassName
= xml
.getStringAttribute ("class", "Component");
108 ((GenericComponent
*) comp
)->constructorParams
= xml
.getStringAttribute ("params", String::empty
);
112 void getEditableProperties (Component
* component
, JucerDocument
& document
, Array
<PropertyComponent
*>& properties
)
114 ComponentTypeHandler::getEditableProperties (component
, document
, properties
);
116 properties
.add (new GenericCompClassProperty (dynamic_cast <GenericComponent
*> (component
), document
));
117 properties
.add (new GenericCompParamsProperty (dynamic_cast <GenericComponent
*> (component
), document
));
120 const String
getClassName (Component
* comp
) const
122 return ((GenericComponent
*) comp
)->actualClassName
;
125 const String
getCreationParameters (Component
* comp
)
127 return ((GenericComponent
*) comp
)->constructorParams
;
130 void fillInCreationCode (GeneratedCode
& code
, Component
* component
, const String
& memberVariableName
)
132 ComponentTypeHandler::fillInCreationCode (code
, component
, memberVariableName
);
134 if (component
->getName().isNotEmpty())
136 << memberVariableName
<< "->setName ("
137 << quotedString (component
->getName())
140 code
.constructorCode
<< "\n";
144 class GenericCompClassProperty
: public ComponentTextProperty
<GenericComponent
>
147 GenericCompClassProperty (GenericComponent
* comp
, JucerDocument
& document
)
148 : ComponentTextProperty
<GenericComponent
> ("class", 300, false, comp
, document
)
152 void setText (const String
& newText
)
154 document
.perform (new GenericCompClassChangeAction (component
, *document
.getComponentLayout(),
155 makeValidCppIdentifier (newText
, false, false, true)),
156 "Change generic component class");
159 const String
getText() const
161 return component
->actualClassName
;
165 class GenericCompClassChangeAction
: public ComponentUndoableAction
<GenericComponent
>
168 GenericCompClassChangeAction (GenericComponent
* const comp
, ComponentLayout
& layout
, const String
& newState_
)
169 : ComponentUndoableAction
<GenericComponent
> (comp
, layout
),
172 oldState
= comp
->actualClassName
;
178 getComponent()->setClassName (newState
);
186 getComponent()->setClassName (oldState
);
191 String newState
, oldState
;
195 class GenericCompParamsProperty
: public ComponentTextProperty
<GenericComponent
>
198 GenericCompParamsProperty (GenericComponent
* comp
, JucerDocument
& document
)
199 : ComponentTextProperty
<GenericComponent
> ("constructor params", 1024, true, comp
, document
)
203 void setText (const String
& newText
)
205 document
.perform (new GenericCompParamsChangeAction (component
, *document
.getComponentLayout(), newText
),
206 "Change generic component class");
209 const String
getText() const
211 return component
->constructorParams
;
215 class GenericCompParamsChangeAction
: public ComponentUndoableAction
<GenericComponent
>
218 GenericCompParamsChangeAction (GenericComponent
* const comp
, ComponentLayout
& layout
, const String
& newState_
)
219 : ComponentUndoableAction
<GenericComponent
> (comp
, layout
),
222 oldState
= comp
->constructorParams
;
228 getComponent()->setParams (newState
);
236 getComponent()->setParams (oldState
);
241 String newState
, oldState
;
247 #endif // __JUCER_GENERICCOMPONENTHANDLER_JUCEHEADER__